package com.kunchi.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.kunchi.platform.entity.project.Project;
import com.kunchi.platform.entity.project.ProjectConf;
import com.kunchi.platform.entity.project.ProjectModule;
import com.kunchi.platform.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * Created by denniszhou on 2017/7/16.
 */
@Controller
@RequestMapping(value = "/project")
public class ProjectController {
    @Autowired
    ProjectService projectService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public List<Project> findByCompanyId(@RequestParam(name = "company_id") Long companyId) {
        return projectService.findByCompanyId(companyId);
    }

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ResponseBody
    public Project findById(@RequestParam(name = "id") Long id) {
        return projectService.findById(id);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Project createProject(@RequestBody JSONObject jsonObject) {
        Project project = JSONObject.parseObject(jsonObject.toJSONString(), Project.class);
        return projectService.createProject(project);
    }

    @RequestMapping(value = "/configuration/create", method = RequestMethod.POST)
    @ResponseBody
    public ProjectConf createConfiguration(@RequestBody JSONObject jsonObject) {
        ProjectConf projectConf = JSONObject.parseObject(jsonObject.toJSONString(), ProjectConf.class);
        return projectService.createConfiguration(projectConf);
    }

    @RequestMapping(value = "/configuration/save", method = RequestMethod.POST)
    @ResponseBody
    public ProjectConf saveConfiguration(@RequestBody JSONObject jsonObject) {
        ProjectConf projectConf = JSONObject.parseObject(jsonObject.toJSONString(), ProjectConf.class);
        return projectService.saveConfiguration(projectConf.getId(), projectConf.getContent(), projectConf.getPicture());
    }

    @RequestMapping(value = "/configuration/delete", method = RequestMethod.GET)
    public void deleteConfiguration(@RequestParam(name = "id") Long id) {
        projectService.deleteConfiguration(id);
    }

    @RequestMapping(value = "/configuration/find", method = RequestMethod.GET)
    @ResponseBody
    public Object findConfigurationByProjectId(
            @RequestParam(name = "id", required = false) Long id,
            @RequestParam(name = "project_id") Long projectId) {
        if (Objects.isNull(id)) {
            return projectService.findConfigurationsByProjectId(projectId);
        } else {
            return projectService.findConfigurationByIdAndProjectId(id, projectId);
        }
    }

    @RequestMapping(value = "/module/find", method = RequestMethod.GET)
    @ResponseBody
    public ProjectModule findProjectModuleByProjectId(@RequestParam(name = "project_id") Long projectId) {
        return projectService.findProjectModuleByProjectId(projectId);
    }

    @RequestMapping(value = "/module/update", method = RequestMethod.POST)
    @ResponseBody
    public ProjectModule updateProjectModule(@RequestBody JSONObject jsonObject) {
        ProjectModule projectModule = JSONObject.parseObject(jsonObject.toJSONString(), ProjectModule.class);
        return projectService.updateProjectModule(projectModule);
    }
}
